/*
 * @(#) $Header$
 *
 * Copyright (C) 2013  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.util.Iterator;
import ca.forklabs.baselib.util.UnaryFunction;

/**
 * Class {@code TransformerIterator} applies a transformation on the object
 * before returning it.
 *
 * @param   <R>   the type after the transformation.
 * @param   <A>   the type before the transformation.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.TransformerIterator">Daniel Léonard</a>
 * @version $Revision$
 */
public class TransformerIterator<R, A> implements Iterator<R> {

//---------------------------
// Instance variables
//---------------------------

   /** The transforming function. */
   private UnaryFunction<R, A> function;

   /** The source iterator. */
   private Iterator<A> iterator;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @param   function   the transforming function.
    * @param   iterator   the source iterator.
    */
   public TransformerIterator(UnaryFunction<R, A> function, Iterator<A> iterator) {
      this.setFunction(function);
      this.setIterator(iterator);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gives the transforming function.
    * @return   the function.
    */
   protected UnaryFunction<R, A> getFunction() {
      return this.function;
      }

   /**
    * Changes the transforming function.
    * @param   function   the new function.
    */
   protected void setFunction(UnaryFunction<R, A> function) {
      this.function = function;
      }

   /**
    * Gives the source iterator.
    * @return   the iterator.
    */
   protected Iterator<A> getIterator() {
      return this.iterator;
      }

   /**
    * Changes the source iterator.
    * @param   iterator   the new iterator.
    */
   protected void setIterator(Iterator<A> iterator) {
      this.iterator = iterator;
      }


//---------------------------
// Implemented methods from java.util.Iterator
//---------------------------

   /**
    * {@inheritDoc}
    */
   @Override
   public R next() {
      UnaryFunction<R, A> f = this.getFunction();
      Iterator<A> i = this.getIterator();
      A a = i.next();
      R r = f.invoke(a);
      return r;
      }

   /**
    * {@inheritDoc}
    */
   @Override
   @SuppressWarnings("hiding")
   public boolean hasNext() {
      Iterator<A> iterator = this.getIterator();
      boolean has_next = iterator.hasNext();
      return has_next;
      }

   /**
    * {@inheritDoc}
    */
   @Override
   @SuppressWarnings("hiding")
   public void remove() {
      Iterator<A> iterator = this.getIterator();
      iterator.remove();
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a new transformer iterator.
    * @param   function   the transforming function.
    * @param   iterator   the source iterator.
    * @return  a new transformer iterator.
    */
   public static <R, A> TransformerIterator<R, A> create(UnaryFunction<R, A> function, Iterator<A> iterator) {
      TransformerIterator<R, A> transformer = new TransformerIterator<>(function, iterator);
      return transformer;
      }

   }
